package ibmetp.mis.controller.role;

import ibmetp.mis.common.Constants;
import ibmetp.mis.json.DateJsonValueProcessor;
import ibmetp.mis.service.PageService;
import ibmetp.mis.serviceImpl.RoleServiceImpl;
import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.Role;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

/**
 * Servlet implementation class RolePageServlet
 */
public class RolePageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RolePageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding(Constants.ENCODING_UTF8);
		//当前页号
		int pageNo=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
		System.out.println("pageNo:"+pageNo);
		
		//页面大小
		int pageSize=request.getParameter("rows")!=null?Integer.parseInt(request.getParameter("rows")):2;
		System.out.println("pageSize:"+pageSize);
		
		//查询条件Role
        Role role=new Role();
        if(request.getParameter("roleId")!=null)
        {
		   role.setRoleId(Long.valueOf(request.getParameter("roleId")));
        }
		role.setRoleName(request.getParameter("roleName"));
		//System.out.println("SearchConditions:"+role);
		
		//查询数据
		PageService us=new RoleServiceImpl();
		Page p=us.getPage(pageNo, pageSize, role);	   
		
		//输出JSON数据
		JSONObject o=new JSONObject();
		//若对象中含有日期对象采用
		 JsonConfig jsonConfig = new JsonConfig();  
	     jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));  
	   //若无的话：o.put("rows",p.getList());		
		o.put("rows",JSONSerializer.toJSON(p.getList() , jsonConfig).toString());
		o.put("total",p.getTotal());
		response.setCharacterEncoding(Constants.ENCODING_UTF8);
		response.getWriter().write(o.toString());
	}
	

}
